package com.enqualcomm.kids.view;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.enqualcomm.kids.R;

public class ChartItem extends LinearLayout {

	private TextView desc;
	private View progressView;
	private View secondProgressView;
	private int max;
	private int progress;
	private float descHeight;
	private static final int SECOND_PROGRESS_MAX = 10000;
	

	public ChartItem(Context context, float padding, float descHeight, boolean secondProgress) {
		super(context);
		setOrientation(VERTICAL);
		setGravity(Gravity.BOTTOM);
		setFocusable(true);
		setClickable(true);
		
		this.descHeight = descHeight;
		if (descHeight>0) {
			desc = new TextView(context);
			desc.setTextSize(10);
			desc.setGravity(Gravity.CENTER_HORIZONTAL);
			desc.setMaxLines(1);
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,(int) descHeight);
			addView(desc,params);
		}
		
		if (secondProgress) {
			LinearLayout progressView = new LinearLayout(context);
			progressView.setOrientation(VERTICAL);
			progressView.setGravity(Gravity.BOTTOM);
			//添加secondProgress
			secondProgressView = new View(context);
			secondProgressView.setBackgroundResource(R.drawable.selector_chart_progress3);
			progressView.addView(secondProgressView,new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
			this.progressView = progressView;
		}else {
			progressView = new View(context);
		}
		
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		params.leftMargin = params.rightMargin = (int) (padding/2);
		addView(progressView, params);
	}
	
	public void setMax(int max){
	     if (max < 0) {
	         max = 0;
	     }
		this.max = max;
	}
	
	private void refreshProgress() {
		//计算progressView的高度
		int height = 0;
		if (max==0||progress==0) {
//			height = 0;
		}else{
			height = (int) (((float)progress/max)*(getHeight()-descHeight));
		}
		LayoutParams params = (LayoutParams) progressView.getLayoutParams();
		params.height = height;
		progressView.setLayoutParams(params);
	}
	
	private void refreshProgress2() {
		int height = 0;
		int secondHeight = 0;
		if (max==0||progress==0) {
		
		}else {
			if (progress<SECOND_PROGRESS_MAX) {
				height = (int) (((float)SECOND_PROGRESS_MAX/max)*(getHeight()-descHeight));
				secondHeight = (int) (((float)progress/SECOND_PROGRESS_MAX)*height);
			}else {
				height = (int) (((float)progress/max)*(getHeight()-descHeight));
//				secondHeight = 0;
			}
		}
		
		LayoutParams params = (LayoutParams) progressView.getLayoutParams();
		params.height = height;
		progressView.setLayoutParams(params);
		
		params = (LayoutParams) secondProgressView.getLayoutParams();
		params.height = secondHeight;
		secondProgressView.setLayoutParams(params);
	}
	
	
	public void setProgress(int progress){
	    if (progress < 0) {
            progress = 0;
        }
        if (progress > max) {
            progress = max;
        }
        
    	this.progress = progress;
    	if (secondProgressView!=null) {
    		refreshProgress2();
		}else {
			refreshProgress();
		}
	}
	
	public void setProgressBackground(int resId){
		progressView.setBackgroundResource(resId);
	}
	
	public int getProgress(){
		return progress;
	}
	
	public void setDesc(String str){
		if (desc!=null) {
			desc.setText(str);
		}
	}

	public void setDescColor(int color) {
		if (desc!=null) {
			desc.setTextColor(getResources().getColorStateList(color));
		}
	}
	
}
